package com.javacode2018.demo2;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@ComponentScan
@EnableTransactionManagement(proxyTargetClass = true)
public class MainConfig2 {
    @Bean
    public DataSource dataSource1(DsProperties dsProperties) {
        MyDataSource dataSource = new MyDataSource("dataSource1");
        BeanUtils.copyProperties(dsProperties, dataSource);
        System.out.println(dataSource);
        return dataSource;
    }

    @Bean
    public DataSource dataSource2(DsProperties dsProperties) {
        MyDataSource dataSource = new MyDataSource("dataSource2");
        BeanUtils.copyProperties(dsProperties, dataSource);
        System.out.println(dataSource);
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate1(@Qualifier("dataSource1") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public JdbcTemplate jdbcTemplate2(@Qualifier("dataSource2") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public DataSourceTransactionManager transactionManager1(@Qualifier("dataSource1") DataSource dataSource) {
        return new MyDataSourceTransactionManager(dataSource,"transactionManager1");
    }

    @Bean
    public DataSourceTransactionManager transactionManager2(@Qualifier("dataSource2") DataSource dataSource) {
        return new MyDataSourceTransactionManager(dataSource,"transactionManager2");
    }
}
